// packageUser/pages/evaluate/pay-evaluate.js
import OrderService from '../../../service/OrderService';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    businessId: '',
    score: 0,
    evaluate: {},
    errMsg:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.orderService = new OrderService();
    const { id, businessId } = options;
    this.setFieldValue("orderId", id);
    this.setData({ businessId })
  },
  setFieldValue: function (field, value) {
    const { evaluate } = this.data;
    evaluate[field] = value;
    this.setData({ evaluate });
  },
  onScoreChage: function(e){
    this.setData({ score: e.detail.value })
  },
  onContentChange: function(e){
    this.setFieldValue("content", e.detail.value);
  },
  onImagesChange: function(e){
    const images = e.detail.images;
    const fileList = images.map(item => {
      return { fileUrl: item };
    })
    this.setFieldValue("fileList", fileList);
  },
  validForm: function (form) {
    if (!form.orderId) {
      this.setData({ errMsg: '订单ID为空' })
      return false;
    }
    if (!form.content) {
      this.setData({ errMsg: '评价内容不能为空' })
      return false;
    }
    this.setData({ errMsg: '' })
    return true;
  },
  submit: function (e) {
    const { businessId, score, evaluate } = this.data;
    if (!businessId) {
      this.setData({ errMsg: '订单方ID为空' })
      return;
    }
    if (!this.validForm(evaluate)){
      return;
    }
    this.orderService.payEval({ businessId, score, evaluate }).then(res => {
      if (res.status == 0) {
        wx.navigateBack()
      }
    })
  }
})